<?php

namespace App\Repositories\Manage;

use App\Models\DailyMenu;
use App\Models\MenuProductMapping;

class MenuProductMappingRepository
{

    public function getList($filter = [], $pageNum = 2, $pageSize = 10): array
    {
        $res = DailyMenu::from("daily_menu as dm")
            ->join("supplier as s", "s.id", "dm.supplier_id")
            ->when(!empty($filter['supplier_id']), function ($query) use ($filter) {
                return $query->where("dm.id", $filter['supplier_id']);
            })
            ->when((!empty($filter['stype']) || strlen($filter['stype']) > 0), function ($query) use ($filter) {
                return $query->where("dm.type", intval($filter['stype']));
            })
            ->orderBy("dm.date", "desc")
            ->orderBy("dm.id", "desc")
            ->paginate($pageSize, ["dm.*", "s.title as supplier_title"], null, $pageNum)
            ->toArray();

        $res = filterPaginateData($res);

        foreach ($res['items'] as $k => &$v) {
            $v['product_amount'] = MenuProductMapping::from("menu_product_mapping as mpm")
                ->join("product as p", "p.id", "=", "mpm.product_id")
                ->where([
                    'p.status' => 1,
                    'mpm.status' => 1,
                    'mpm.menu_id' => $v['id']
                ])
                ->count("mpm.id");
        }
        return $res;
    }

    /**
     * 菜单对应的产品
     * @param int $menu_id
     * @return array|void
     */
    public function getIdsByMenuId($menu_id = 0)
    {
        if (empty($menu_id))
            return [];


    }

    /**
     * 添加菜单产品
     * @param $menu_id
     * @param null $productIds
     * @param null $comboIds
     */
    public function addProductMapping($menu_id = 0, $productIds = null, $comboIds = null)
    {
        if (!$menu_id)
            return retFail("菜单异常");

        $menu_product_data = [];
        foreach ($productIds as $k => $v) {
            $v = array_filter(explode("-", $v));
            if (count($v) != 2) continue;

            $menu_product_data[] = [
                'type' => 0,
                'category_id' => $v[0],
                'product_id' => $v[1],
                'menu_id' => $menu_id,
                'create_time' => time(),
                'update_time' => time(),
            ];
        }

        MenuProductMapping::insert($menu_product_data);

        //删除菜单缓存
        delRedis("menuCateList_" . $menu_id);

        return retSuc([], "添加成功");
    }

    public function getOne($id = null)
    {
        return DailyMenu::query()->where("id", $id)->first();
    }

    //更新菜单缓存
    public function updateMenuRedisById($product_id = 0)
    {
        if (!intval($product_id))
            return true;

        $menuIds = MenuProductMapping::query()->where("product_id", $product_id)->pluck("menu_id")->toArray();
        if (empty($menuIds) && count($menuIds) == 0)
            return true;

        $menuIds = array_unique($menuIds);
        foreach ($menuIds as $menu_id) {
            delRedis("menuCateList_" . $menu_id);
        }

        return true;
    }

}
